﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using ltModel;
using Telerik.Web.UI;

public partial class ltMan_news_newsList : AdminBasePage
{
    ltModel.BLL.article_content acModel = new ltModel.BLL.article_content();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindTreeView(sClass, false);
            BindTreeView(sClassSearch, true);
            sDate.SelectedDate = DateTime.Now;
        }
        BindData();
    }
    void BindData()
    {
        SqlPager pager = new SqlPager("v_article_content_with_cls", "news_id", "*");
        StringBuilder where = new StringBuilder();
        if (!CurrentUser.IsManager)
            where.AppendFormat(" faburen='{0}'", CurrentUser.username);
        if (!string.IsNullOrEmpty(sTitle.Text))
        {
            if (where.Length > 0)
                where.Append(" and ");
            where.AppendFormat(" title like '%{0}%'", sTitle.Text.Replace("'", "''"));
        }
        string ws = Request.QueryString["ws"];
        if (ws == "1")
        {
            if (where.Length > 0)
                where.Append(" and ");
            where.Append(" sh=0 ");
        }
        if (sClassSearch.SelectedValue != "0" &&
            Str.IsInt(sClassSearch.SelectedValue))
        {//查询类别
            if (where.Length > 0)
                where.Append(" and ");
            where.Append(" classid in(");
            where.Append(sClassSearch.SelectedValue);
            RadTreeView treeview = sClassSearch.Items[0].FindControl("RadTreeView1") as RadTreeView;
            IList<RadTreeNode> selectedNodes = treeview.FindNodeByValue(sClassSearch.SelectedValue).GetAllNodes();
            foreach (RadTreeNode tmpNode in selectedNodes)
            {
                where.Append(",");
                where.Append(tmpNode.Value);
            }/*
            IList<RadTreeNode> selectedNodes = treeview.FindNodeByValue(sClassSearch.SelectedValue).GetAllNodes();
            int l = selectedNodes.Count - 1;
            for (int i = 0; i <= l; i++)
            {
                where.Append(",");
                where.Append(selectedNodes[i].Value);
            }*/
            where.Append(") ");
        }
        pager.Where = where.ToString();
        pager.OrderBy = "dat desc";
        RadGridBinder.Create(RadGrid1, pager);
    }
    void BindTreeView(RadComboBox box, bool inSearch)
    {
        string sql = string.Format("select '0' as class_id,'{0}' as classname,'0' as father_id union all select class_id,classname,father_id from lt_article_class", inSearch ? "所有类别" : "选择类别");
        if (!CurrentUser.IsManager)
        {
            sql += string.Format(" where Class_id in ({0}) ", CurrentUserGroup.GetLmQx());
        }
        RadTreeView treeview = box.Items[0].FindControl("RadTreeView1") as RadTreeView;
        treeview.DataSource = SqlHelper.Instance.ExecuteReader(sql);
        treeview.DataBind();
        treeview.ExpandAllNodes();
        if (!inSearch)
        {
            foreach (RadTreeNode item in treeview.GetAllNodes())
            {
                if (item.Nodes.Count > 0)
                {
                    item.Enabled = false;
                    item.ForeColor = System.Drawing.Color.Black;
                    item.Font.Italic = true;
                }
            }
        }
    }

    protected void RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e)
    {
        int id = int.Parse(GridCommander.ExtractValue(e, "news_id"));
        ltModel.Model.article_content news = acModel.GetModel(id);
        if (news != null)
        {
            ltModel.BuilderNews.DeleteFile(news.Link);//删除文件
        }
        OperateLog.AddSuccessLog(CurrentUser.user_id, Request.UserHostAddress, "新闻管理-删除>" + news.Title);
        acModel.Delete(id);
    }
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "Verify":
                UpdateShTj(e.CommandArgument.ToString(), string.Empty, e);
                break;
            case "Recommend":
                UpdateShTj(string.Empty, e.CommandArgument.ToString(), e);
                break;
            case "ReBuild":
                OperateLog.AddSuccessLog(CurrentUser.user_id, Request.UserHostAddress, "新闻管理-重新生成所有");
                ltModel.BuilderNews.ReBuildAll();
                break;
        }
    }
    void UpdateShTj(string sh, string tj, GridCommandEventArgs e)
    {
        string ids = GridCommander.JoinValues(e.Item.OwnerTableView.DataKeyValues, RadGrid1.SelectedIndexes);
        if (string.IsNullOrEmpty(sh))
            OperateLog.AddSuccessLog(CurrentUser.user_id, Request.UserHostAddress, "新闻管理-修改推荐>" + ids);
        else
            OperateLog.AddSuccessLog(CurrentUser.user_id, Request.UserHostAddress, "新闻管理-修改审核>" + ids);
        acModel.UpdateShTj(sh, tj, ids, CurrentUser.username);
        acModel.UpdateManSh(CurrentUser.username, 1);
        RadGrid1.Rebind();
    }
    protected void b1_Click(object sender, EventArgs e)
    {
        RadGrid1.Rebind();
    }
    protected void b2_Click(object sender, EventArgs e)
    {
        string ids = GridCommander.JoinValues(RadGrid1.MasterTableView.DataKeyValues, RadGrid1.SelectedIndexes);
        if (string.IsNullOrEmpty(ids))
        {
            RadAlertM("请选择要操作的记录！");
            OperateLog.AddFailedLog(CurrentUser.user_id, Request.UserHostAddress, "新闻管理-修改类别>请选择要操作的记录");
            return;
        }
        if (sClass.SelectedValue == "0")
        {
            RadAlertM("请选择类别！");
            OperateLog.AddFailedLog(CurrentUser.user_id, Request.UserHostAddress, "新闻管理-修改类别>请选择类别");
            return;
        }
        acModel.UpdateClassId(ids, sClass.SelectedValue);
        OperateLog.AddSuccessLog(CurrentUser.user_id, Request.UserHostAddress, "新闻管理-修改类别>" + ids + "->" + sClass.SelectedValue);
        RadGrid1.Rebind();
    }
    protected void b3_Click(object sender, EventArgs e)
    {
        string ids = GridCommander.JoinValues(RadGrid1.MasterTableView.DataKeyValues, RadGrid1.SelectedIndexes);
        string time = sDate.SelectedDate.ToString();
        if (string.IsNullOrEmpty(ids))
        {
            RadAlertM("请选择要操作的记录！");
            OperateLog.AddFailedLog(CurrentUser.user_id, Request.UserHostAddress, "新闻管理-更新时间>请选择要操作的记录");
            return;
        }
        OperateLog.AddSuccessLog(CurrentUser.user_id, Request.UserHostAddress, "新闻管理-更新时间>" + ids + "->" + time);
        acModel.UpdateTime(time, ids);
        RadGrid1.Rebind();
    }
}